package com.example.demospringboot;



import com.example.demospringboot.drwaing.entity.Player;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.*;

@SpringBootTest
class test2 {
        // 每个部门的人数
        private static final int NUM_DEPT = 24;


        public static void main(String[] args) {
            int i = 0;
            while (i==0){
                i = a();
            }
        }
        public static int a() {
            try {
                // 5个主要部门，以及沈阳局和北京局
                List<String> harbin = generateDepartment("哈尔滨局", 4);
                List<String> huhehaote = generateDepartment("呼和浩特局", 5);
                List<String> lanzhou = generateDepartment("兰州局", 5);
                List<String> nanning = generateDepartment("南宁局", 2);
                List<String> qingzang = generateDepartment("青藏公司", 3);
                List<String> shenyang = generateDepartment("沈阳局", 5);
                List<String> beijing = generateDepartment("北京局", 6);
                List<String> taiyuan = generateDepartment("太原局", 22);
                List<String> zhengzhou = generateDepartment("郑州局", 4);
                List<String> wuhan = generateDepartment("武汉局", 13);
                List<String> xian = generateDepartment("西安局",5);
                List<String> jinan = generateDepartment("济南局", 1);
                List<String> shanghai = generateDepartment("上海局", 11);
                List<String> nanchang = generateDepartment("南昌局", 10);
                List<String> guangzhou = generateDepartment("广州局", 5);
                List<String> chengdu = generateDepartment("成都局", 5);
                List<String> kunming = generateDepartment("昆明局", 3);
                List<String> wulu = generateDepartment("乌鲁木齐局", 1);
                List<String> sheji = generateDepartment("设计集团", 4);
                List<String> tehuo = generateDepartment("特货公司", 3);
                List<String> tiekeyuan = generateDepartment("铁科院", 1);
                List<String> xinxizhongxin = generateDepartment("信息中心", 2);

                // 创建总列表，包含所有部门人员
                List<String> allPeople = new ArrayList<>();
                allPeople.addAll(harbin);
                allPeople.addAll(huhehaote);
                allPeople.addAll(lanzhou);
                allPeople.addAll(nanning);
                allPeople.addAll(qingzang);
                allPeople.addAll(shenyang);
                allPeople.addAll(beijing);
                allPeople.addAll(taiyuan);
                allPeople.addAll(zhengzhou);
                allPeople.addAll(wuhan);
                allPeople.addAll(xian);
                allPeople.addAll(jinan);
                allPeople.addAll(shanghai);
                allPeople.addAll(nanchang);
                allPeople.addAll(guangzhou);
                allPeople.addAll(chengdu);
                allPeople.addAll(kunming);
                allPeople.addAll(wulu);
                allPeople.addAll(sheji);
                allPeople.addAll(tehuo);
                allPeople.addAll(tiekeyuan);
                allPeople.addAll(xinxizhongxin);
                System.out.println(allPeople.size());

                // 对所有人员进行随机打乱
                Collections.shuffle(allPeople);

                // 记录每个部门新的成员
                List<String> harbinNew = new ArrayList<>();
                List<String> huhehaoteNew = new ArrayList<>();
                List<String> lanzhouNew = new ArrayList<>();
                List<String> nanningNew = new ArrayList<>();
                List<String> qingzangNew = new ArrayList<>();

                // 用于记录已经被分配的人员
                Set<String> assignedPeople = new HashSet<>();

                // 按照要求分配每个部门24个人，并且确保不能在原部门
                assignPeople(harbin, harbinNew, allPeople,assignedPeople, NUM_DEPT);
                assignPeople(huhehaote, huhehaoteNew, allPeople,assignedPeople, NUM_DEPT);
                assignPeople(lanzhou, lanzhouNew, allPeople, assignedPeople,NUM_DEPT);
                assignPeople(nanning, nanningNew, allPeople, assignedPeople,NUM_DEPT);
                assignPeople(qingzang, qingzangNew, allPeople,assignedPeople, NUM_DEPT);
                // 输出结果
                printDepartment("哈尔滨局", harbinNew);
                printDepartment("呼和浩特局", huhehaoteNew);
                printDepartment("兰州局", lanzhouNew);
                printDepartment("南宁局", nanningNew);
                printDepartment("青藏公司", qingzangNew);
                return 1;
            }catch (Exception e){
                return 0;
            }

        }


        // 随机生成一个部门的人员
        private static List<String> generateDepartment(String departmentName, int size) {
            List<String> department = new ArrayList<>();
            for (int i = 1; i <= size; i++) {
                department.add(departmentName + "-人员" + i);
            }
            return department;
        }

        // 随机分配7个不在原部门的人员
        private static void assignPeople(List<String> originalDepartment, List<String> newDepartment, List<String> allPeople, Set<String> assignedPeople, int numDept) {
            List<String> temp = new ArrayList<>(allPeople); // 创建副本避免修改原始列表
            temp.removeAll(originalDepartment); // 移除原部门人员

            // 过滤掉已经被分配过的人员
            temp.removeAll(assignedPeople);

            // 打乱剩余人员列表
            Collections.shuffle(temp);

            // 选取前numToAssign个未被分配且不在原部门的人员
            for (int i = 0; i < numDept; i++) {
                String selectedPerson = temp.get(i);
                newDepartment.add(selectedPerson);
                assignedPeople.add(selectedPerson); // 标记该人员已经被分配
            }
        }

        // 输出每个部门的新成员
        private static void printDepartment(String departmentName, List<String> department) {

            System.out.println(departmentName + " 新成员:");
            for (String person : department) {
                System.out.println(person);
            }
            System.out.println();
        }
    }
